//
//  AppDelegate.m
//  Style
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "GeDiaoViewController.h"
#import "YLArticleViewController.h"
#import "HuaBaoViewController.h"
#import "AboutViewController.h"
#import "UMSocial.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Override point for customization after application launch.
    ///**cece*//
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [UMSocialData setAppKey:@"55e26fd6e0f55a666100490e"];
    
    GeDiaoViewController *GeDiao = [[GeDiaoViewController alloc] init];
    UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:GeDiao];
    UIImage *img = [UIImage imageNamed:@"Home"];
    GeDiao.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:img tag:100];
    
    HuaBaoViewController *HuaBao = [[HuaBaoViewController alloc] init];
    UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:HuaBao];
    UIImage *pic = [UIImage imageNamed:@"picture"];
    HuaBao.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"生活有意思" image:pic tag:200];
    
    
    YLArticleViewController *Article = [[YLArticleViewController alloc] init];
    UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:Article];
    UIImage *art = [UIImage imageNamed:@"Article"];
    Article.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"文章" image:art tag:300];
    
    
    AboutViewController *about = [[AboutViewController alloc] init];
    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:about];
    UIImage *aboutimg = [UIImage imageNamed:@"Person"];
    about.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"个人" image:aboutimg tag:100];
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[navi1, navi3, navi2 ,navi4];
    self.tabBarController.tabBar.tintColor = [UIColor blackColor];
    self.window.rootViewController = self.tabBarController;
    
    
    /* 1.找到文件路径(不是文件夹路径) */
    
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *readStrPath = [documentPath stringByAppendingPathComponent:@"location.txt"];
    NSError *error = nil;
    /* 2.创建对象(即读出内容)*/
    NSString *readStr = [NSString stringWithContentsOfFile:readStrPath encoding:NSUTF8StringEncoding error:&error];
    
    /* 判断上次关闭app时, 是否是夜间模式 */
    if ([readStr isEqual:@"on"]) {
        self.window.backgroundColor = [UIColor blackColor];
        self.window.alpha = 0.5;
    }else{
        self.window.backgroundColor = [UIColor whiteColor];
        self.window.alpha = 1;
    }
    
    [self notification];
    

    return YES;
}

#pragma mark - 知识点1 NSNotificationCenter(通知中心)
- (void)notification
{
    /* 创建通知中心对象 */
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    /*
     * 向通知中心注册一个通知
     * 参数1:注册一个通知的观察者
     * 参数2:观察者执行的方法
     * 参数3:注册的通知名字(区别不同的通知)
     * 参数4:过滤通知的发送者, nil任何发送者都可以
     * 注意:观察者的方法, 如果带参数, 参数类型为NSNotification
     */
    [center addObserver:self selector:@selector(moon:) name:@"moon" object:nil];
}

#pragma mark - 实现通知中心观察者方法
- (void)moon:(NSNotification *)notification
{
    if (self.window.backgroundColor == [UIColor whiteColor]) {
        self.window.backgroundColor = notification.object;
        self.window.alpha = 0.5;
    }
    else{
        self.window.backgroundColor = notification.object;
        self.window.alpha = 1;
    }
}


#pragma mark -  夜间模式
- (void)Night:(NSNotification *)notification
{
    if (self.window.backgroundColor == [UIColor whiteColor]) {
        self.window.backgroundColor = notification.object;
        self.window.alpha = 0.6;
    }
    else{
        self.window.backgroundColor = notification.object;
        self.window.alpha = 1;
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
